package com.ohs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ohs.mapper.before.AddressMapper;
import com.ohs.mapper.before.DistrictMapper;
import com.ohs.pojo.Address;
import com.ohs.pojo.District;
import com.ohs.service.AddressService;
import com.ohs.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired(required = false)
    private DistrictMapper districtMapper;

    @Override
    public ResponseResult saveAddress(Address address, HttpSession session) {
        if(address.getProvince().equals("---请选择省份---")){
            return new ResponseResult(207,"请输入省份");
        }
        if(address.getCity().equals("请选择城市...")){
            return new ResponseResult(208,"请输入城市");
        }
        if(address.getArea().equals("请选择县或区...")){
            return new ResponseResult(209,"请选择县或区");
        }
         int row =this.addressMapper.save(address);
         if(row!=1){
             return new ResponseResult(201,"添加地址失败");
         }
        return new ResponseResult(200,"添加地址成功");
    }

    @Override
    public ResponseResult updateAddress(Address address, HttpSession session) {
        if(address.getProvince().equals("---请选择省份---")){
            return new ResponseResult(207,"请输入省份");
        }
        if(address.getCity().equals("请选择城市...")){
            return new ResponseResult(208,"请输入城市");
        }
        if(address.getArea().equals("请选择县或区...")){
            return new ResponseResult(209,"请选择县或区");
        }
        int row =this.addressMapper.updateById(address);
        if(row!=1){
            return new ResponseResult(201,"修改地址失败");
        }
        return new ResponseResult(200,"修改地址成功");
    }

    @Override
    public ResponseResult deleteAddress(Integer addId, HttpSession session) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addId",addId);
        int row= this.addressMapper.delete(queryWrapper);
        if(row!=1){
            return new ResponseResult(201,"删除失败");
        }
        return new ResponseResult(200,"删除成功");
    }

    @Override
    public ResponseResult findAllAddressInfo() {
        List<Address> addressList= this.addressMapper.selectAllAddressInfo();
        if(addressList.size()==0){
            return new ResponseResult(201,"请添加收货地址");
        }
        for(int i=0;i<addressList.size();i++){
            Address address = addressList.get(i);
            String province= address.getProvince();
            String city=address.getCity();
            String area=address.getArea();
            District district= this.districtMapper.findDistrictByCode(province);
            String provinceName= district.getName();
            District district1= this.districtMapper.findDistrictByCode(city);
            String cityName= district1.getName();
            District district2= this.districtMapper.findDistrictByCode(area);
            String areaName= district2.getName();
            String detail=address.getDetail();
            String detailName=provinceName+cityName+areaName+detail;
            address.setDetail(detailName);
            addressList.set(i,address);
        }
        return new ResponseResult(200,addressList);
    }

    @Override
    public ResponseResult setDefault(Integer addId, HttpSession session) {
        if(addId==null){
            return new ResponseResult(201,"该数据可能已被删除");
        }
        Integer userId= (Integer)session.getAttribute("userId");
        int row= this.addressMapper.setNoneDefault(userId);
        int row1=this.addressMapper.setDefault(addId);
        if(row1!=1){
            return new ResponseResult(203,"操作失败，请重新尝试");
        }
        return new ResponseResult(200,"操作成功！");
    }

    @Override
    public Address findAddressOne(Integer addId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addId",addId);
        Address address= this.addressMapper.selectOne(queryWrapper);
        return address;
    }
}
